package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    //权限属性未定义
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")QueryObject qo, Model model){
        model.addAttribute("result",roleService.query(qo));
        return "role/list";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){
        //查询所有的权限信息显示在页面中
        model.addAttribute("permissions",permissionService.list());
       if(id != null){
            model.addAttribute("entity",roleService.get(id));
        }
        return "role/input";
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Role entity,Long [] ids){
        try{
            if(entity.getId() == null){
                this.roleService.saveOrUpdate(entity,ids);
            }else{
                this.roleService.saveOrUpdate(entity,ids);
            }
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            roleService.delete(id);
            return new JsonResult("操作成功", true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }
}
